package com.alu4r.atcrowdfunding.manager.service.impl;

import com.alu4r.atcrowdfunding.VO.Data;
import com.alu4r.atcrowdfunding.bean.Permission;
import com.alu4r.atcrowdfunding.bean.Role;
import com.alu4r.atcrowdfunding.bean.User;
import com.alu4r.atcrowdfunding.exception.LoginFailException;
import com.alu4r.atcrowdfunding.exception.RegisterFailException;
import com.alu4r.atcrowdfunding.manager.dao.UserMapper;
import com.alu4r.atcrowdfunding.manager.service.UserService;
import com.alu4r.atcrowdfunding.util.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryUserlogin(Map<String, Object> map) {
        User user = userMapper.queryUserLogin(map);
        if (user == null) {
            throw new LoginFailException("用户名或者密码错误");
        }
        return user;
    }

    @Override
    public Boolean insertUserRegister(User user) {
        String userpswd = user.getUserpswd();
        user.setUserpswd(DigestUtils.md5Hex(userpswd));//密码加密
        //设置创建时间
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreatetime(sdf.format(data));
        System.out.println(user.toString());
        int i = userMapper.insert(user);
        System.out.println("*********************" + i);
        if (i < 1) {
            throw new RegisterFailException("用户名或者密码错误");
        }
        return i > 0;
    }

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);//查询多少页，每页多少数据
        List<User> list = userMapper.selectAll();
        PageInfo page = new PageInfo(list, Constant.NAVIGATE_PAGES);//显示5也
        return page;
    }

    @Override
    public PageInfo getPage(Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectByParameter(map);
        PageInfo page = new PageInfo(list, Constant.NAVIGATE_PAGES);
        return page;
    }

    /**
     * 超级用户添加，默认密码为123456
     *
     * @param user
     * @return
     */
    @Override
    public boolean insertUser(User user) {
        //密码加密
        user.setUserpswd(DigestUtils.md5Hex("123456"));
        //设置创建时间
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreatetime(sdf.format(data));

        return userMapper.insert(user) > 0;
    }

    @Override
    public User getUser(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateUser(User user) {
        int i = userMapper.updateUserPrimary(user);
        return i > 0;
    }

    @Override
    public boolean deleteUser(Integer id) {
        int i = userMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public List<Integer> queryRoleByUserId(Integer id) {
        List<Integer> list = userMapper.queryRoleByUserId(id);
        return list;
    }

    @Override
    public List<Role> queryAllRole() {

        return userMapper.queryAllRole();
    }

    @Override
    public boolean deleteBatch(Integer[] ids) {
        int count = userMapper.deleteBatchByKey(Arrays.asList(ids));
        return ids.length == count;
    }

    @Override
    public boolean insertUserRoleRelationship(Integer userId,Data data) {
        try {
            userMapper.insertUserRoleRelationship(userId, data);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteUserRoleRelationship(Integer userId, Data data) {
        try {
            userMapper.deleteUserRoleRelationship(userId,data);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Permission> getPermissionByUserId(Integer id) {
        return userMapper.getPermissionByUserId(id);
    }
}
